package com.spring.tools;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class redisdemo {
    public static void test1(Jedis jedis){
        System.out.println("get 和 set 使用");
        //先清空数据库
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");

        SetParams params = new SetParams();
        params.ex(10);
        params.nx();
        jedis.set("key","333",params);
    }
    public static void test2(Jedis jedis){
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");
        jedis.set("key3","333");

        boolean result = jedis.exists("key");
        System.out.println("result: "+ result);

        long result2 = jedis.del("key");
        System.out.println("result2:" + result2);

        result = jedis.exists("key");
        System.out.println("result:"+result);

        result2 = jedis.del("key","key2","key3");
        System.out.println("result2:"+result2);

    }
    public static void test3(Jedis jedis){
        System.out.println("keys");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.set("key2","222");
        jedis.set("key3","333");
        jedis.set("key4","444");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }
    public static void test4(Jedis jedis){
        System.out.println("expire 和 ttl");
        jedis.flushAll();

        jedis.set("key","111");
        jedis.expire("key",10);

        try{
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long time = jedis.ttl("key");
        System.out.println("time:"+ time);

    }
    public static void test5(Jedis jedis){
        System.out.println("type");
        jedis.flushAll();

        jedis.set("key","111");
        String type = jedis.type("key");
        System.out.println("type:"+type);

    }


    public static void main(String[] args) {
        //连接redis服务器
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()){
            //redis的各种命令,就能对应到jedis对象的各种方法
            /*String pong = jedis.ping();
            System.out.println(pong);*/

            test4(jedis);
        }
    }

}
